<?php

namespace App\Console\Commands;

use App\Models\Goods;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use danielme85\Geoip2\Facade\Reader;
use Illuminate\Support\Facades\Storage;

class BockershopCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'bockershop';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '采集商品';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    //14-11-18
    public function handle()
    {
        $url = "https://kaola.bockers.cc/api/Goods/getCategoryGoodsList?lang=en-us&?t=1712150038877";
        $param = [
            "cate_id" => 14,
            "api_token" => "f48e4fe4a2d1993e47493bba6490aac8",
            "client_id" => 1,
            "token" => "",
            "device_token" => ""
        ];
        for ($i = 5;$i<=18;$i++){
            $param['page'] = $i;
            $this->info("page:".$i);
            $response = Http::asJson()->post($url, $param);
            $result = json_decode($response->body(), true);
            $list = $result['data']['goodres'];
            if(!empty($list)){
                foreach ($list as $goods){

                    $data['price'] = $goods['zs_price'] * 100;
                    if($data['price'] < 5000) {continue; }

                    $data['name'] = $goods['goods_name'];
                    if(Goods::where('name',$data['name'])->first()){$this->info("333");continue; }

                    $tikiimg = $goods['thumb_url'];
                    try {
                        $pathifo = pathinfo($tikiimg);
                        if(isset($pathifo['extension'])){
                            $extension = pathinfo($tikiimg)['extension'];
                        }else{
                            $extension = "jpg";
                        }

                        $path = '/titshop/uploads/cai/' . Carbon::now()->format('YmdH') .'/'.Carbon::now()->format('s').'/'. getrandstr(24).'.'.$extension;
                        $file = (new Client())->get($tikiimg)->getBody();
                    }catch (\Throwable $throwable) {
                        $this->info("111");
                        continue;
                    }
                    if($file){
                        $file2 = Storage::put($path, $file);
                        if($file2){
                            $data['img'] = $path;
                        }
                    }
                    //dump($data);
                    if(empty($data['img'])){$this->info("222"); continue; }


                    $data['profit'] = rand(intval($data['price'] * 0.05) , intval($data['price'] * 0.25));
                    $data['original_price'] = bcsub($data['price'],$data['profit']);
                    $data['category_id'] = 11;
                    $data['code'] = getrandstr(2).rand(100000,999999);
                    $data['status'] = 1;
                    $data['quality'] = 99999;


                    DB::table("goods")->insert($data);

                }
            }
        }

    }









}
